﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Plusii.iiWeb.Framework;
using RLPUR.Common;
using RLPUR.Models;

namespace RLPUR.Web
{
    public partial class PayBalanceEdit : LocalDialog
    {
        #region 属性

        /// <summary>
        /// 当前ID
        /// </summary>
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        #endregion

        protected override string QueryStringValidationPattern
        {
            get
            {
                return string.Empty; //关闭非法参数过滤
            }
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            using (iiConfig configProvider = new iiConfig())
            {
                var rType = configProvider.GetList("RType");
                LocalGlobal.BindListItems(PayType, rType.DefaultView, "Name", "Key", true);
            }

            #endregion

            if (this.CurrentID.Length == 0)
            {
                //新增
            }
            else
            {
                //编辑
                //this.BindItem();
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定项目
        /// </summary>
        private void BindItem()
        {
            using (PurProvider purProvider = new PurProvider())
            {
            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (PurProvider purProvider = new PurProvider())
                {
                    decimal payAmt = Util.ToDecimal(PayAmt.Text.Trim());
                    if (payAmt == 0)
                    {
                        this.ShowWarningMessage("金额不正确");
                        return;
                    }

                    if (this.CurrentID.Length == 0)
                    {
                        this.ShowWarningMessage("参数错误");
                        return;
                    }

                    var item = purProvider.GetPayBalanceDetail(this.CurrentID);
                    if (item == null)
                    {
                        this.ShowWarningMessage("参数错误");
                        return;
                    }

                    decimal remainAmt = Util.ToDecimal(item["remainAmt"].ToString());
                    if (payAmt > remainAmt)
                    {
                        this.ShowWarningMessage("付款金额不能大于未付金额");
                        return;
                    }
                    purProvider.InsertPayBalanceSql(this.CurrentID, PayDate.Text.Trim(), PayAmt.Text.Trim(),
                        PayType.SelectedValue.Trim(), LocalGlobal.CurrentUserID, LocalGlobal.CurrentUser);
                }
            }
            catch (Exception error)
            {
                this.ShowErrorMessage("保存失败。" + error.Message);
                return;
            }
            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }

        #endregion
    }
}